package com.lanou.fooddemo.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.TextView;
import com.lanou.fooddemo.R;
import com.lanou.fooddemo.bean.DataBean;
import com.lanou.fooddemo.external.smart_image_view.SmartImageView;
import com.lanou.fooddemo.fragment.RecommendFragment;
import com.lanou.fooddemo.util.NetUtil;
import java.util.List;

public class RecommendInformationActivity extends Activity implements View.OnClickListener {
    private final String LOG_TAG = "RInfoActivity";

    private SmartImageView shopImageView;
    /**
     * 返回按钮
     * */

    private TextView backTextView;

    /**
     *姓名TextView
     */
    private TextView userNameTextView;

    /**
     * 签名TextView
     **/
    private TextView userSignTextView;

    /**
     *头像ImageView
     **/
    private SmartImageView userIcImageView;

    /**
     *食物 ImageView
     **/

    private SmartImageView foodImageView;

    /**
     *内容简介TextView
     */

    private TextView contentTextView;


    private final int DATA = 2;

    /**
     *数据List
     **/
    private List<DataBean> dataBeans;

    /**
     *用户Bean
     **/

    private DataBean userDataBean;

    /**
     * 食物Bean
     * */

     private DataBean foodDataBean;

    /**
     * 简介内容Bean
     **/

    private DataBean contentBean;

    /**
     *店铺内容Bean
     **/

    private DataBean shopDataBean;

    private Handler handler;

    /**
     *点击监听接口声明
     **/
    private View.OnClickListener clickListener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reommend_information);
        //初始化试图
        initView();
        //获取数据
        getData();
    }
    /**
     * 初始化试图
     */
    private void initView(){
        shopImageView = (SmartImageView) findViewById(R.id.recommendActivityFoodImageView);
        backTextView = (TextView) findViewById(R.id.recommendActivity);
        userNameTextView = (TextView) findViewById(R.id.recommendActivityUserName);
        userSignTextView = (TextView) findViewById(R.id.recommendActivityUserSign);
        foodImageView = (SmartImageView) findViewById(R.id.recommendActivityFoodImageView);
        contentTextView = (TextView) findViewById(R.id.recommendActivityContent);
        userIcImageView = (SmartImageView) findViewById(R.id.recommendActivityUserIcImageView);

        //透明动画 从完全不透明到完全透明的一个动画效果
        AlphaAnimation alphaAnimation = new AlphaAnimation
                (0f, 1f);

        alphaAnimation.setDuration(2500);//设置动画持续时间 4s
        alphaAnimation.setFillAfter(false);//设置动画结束之后的效果
        alphaAnimation.setInterpolator(this, android.R.interpolator.accelerate_decelerate);

        //第一种方式
        shopImageView.startAnimation(alphaAnimation);//播放动画


        //实例化点击监听类
        clickListener = new InformationOnClickListener();

        //设置返回按钮点击监听
        backTextView.setOnClickListener(clickListener);

        //实例化Handler,通过自定义Callback类
        handler = new Handler(new InformationCallback());
    }

    /**
     * 获取数据
     */
    private void getData(){
        //实例化InformationRunnabale类
        InformationRunnabale informationRunnabale = new InformationRunnabale();
        //传入InformationRunnabale实例化Thread
        Thread thread = new Thread(informationRunnabale);
        //开启线程
        thread.start();
    }

    @Override
    public void onClick(View v) {

    }

    /**
     * handkler的Callback参数类
     */
    private class InformationCallback implements Handler.Callback {
        @Override
        public boolean handleMessage(Message msg){
         //根据消息不同msg值作出不同处理
            switch (msg.what){
                //处理姓名信息
                case DATA:
                    if (userNameTextView == null) {
                        Log.d("TAGG","userNameTextView is null");
                    }
                    if (userDataBean == null) {
                        Log.d("TAGG","userDataBean is null");
                    }


                    //填写姓名
                    userNameTextView.setText(userDataBean.getTitle());
                    //填充签名
                    userSignTextView.setText(userDataBean.getDesc());
                    //填充头像
                    userIcImageView.setImageUrl(userDataBean.getUser().getAvatar(), SmartImageView.TYPE_ROUND);
                    //填充食物图片
                    foodImageView.setImageUrl(foodDataBean.getPic().getUrl());
                    //填充简介内容数据
                    contentTextView.setText(contentBean.getContent());

                    break;
            }
            return true;
        }
    }

    /**
     * Thread的Runnable参数类
     */
    private class InformationRunnabale implements Runnable{
        /**
         * 记录Bean类型
         */
        String type = null;

        @Override
        public void run(){
            //从网络工具类获取dataBeans
            dataBeans = NetUtil.getDataBeanListDataForRecommendInformation
                    (getIntent().getStringExtra(RecommendFragment.INFORMATION_KEY));
            //如果获取数据不为空
            Log.d("TAGG",dataBeans.size()+"");
            if (null != dataBeans){
                //遍历dataBeans
                for (DataBean dataBean : dataBeans){
                    type = dataBean.getType();
                    //如果是UserCard类型
                    if (DataBean.TYPE_USER_CARD.equals(type)){
                        //获取到UserCard类型的Bean
                        userDataBean = dataBean;
                        Log.d("TAGG",userDataBean.getTitle());
                    }else if (DataBean.TYPE_IMAGE_CARD.equals(type)){
                        //获取ImageCard类型的Bean
                        foodDataBean = dataBean;
                    }else if (DataBean.TYPE_CONTENT_CARD.equals(type)){
                        //获取ContentCard类型的Bean
                        contentBean = dataBean;
                    }else if (DataBean.TYPE_PIC_CARD.equals(type)){
                        //获取PicCard类型的店铺Bean
                        shopDataBean = dataBean;
                    }
                }
                //通过handler获取消息
                Message msg = handler.obtainMessage();
                //设置消息what值
                msg.what = DATA;
                //发送消息
                handler.sendMessage(msg);
            } else {
                Log.i(LOG_TAG, "data is null");
            }
        }
    }

    /**
     * 点击事件监听类
     */
    private class InformationOnClickListener implements View.OnClickListener {
   @Override
        public void onClick(View v){
       switch (v.getId()){
           case  R.id.recommendActivity:
               //销毁当前Activity
               finish();
               break;
       }

   }

    }
}

